<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\PlatformBalanceLogDayDetail;
use App\PlatformBalanceDayLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PlatformBalanceDayLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '对账单-日';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PlatformBalanceDayLog());
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->between('created_at', '日期')->datetime();
        });

        $grid->column('day', '日期');
        $grid->column('account', '账户')->display(function(){return '平台余额';});
        $grid->column('in', '收入')->display(function($value){
            return $value.'<br>'.$this->in_num.'笔';
        });
        $grid->column('out', '支出')->display(function($value){
            return $value.'<br>'.$this->out_num.'笔';
        });

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            // 去掉查看
            $actions->disableView();

            $actions->add(new PlatformBalanceLogDayDetail());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PlatformBalanceDayLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('num', __('Num'));
        $show->field('in', __('In'));
        $show->field('out', __('Out'));
        $show->field('day', __('Day'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PlatformBalanceDayLog());

        $form->number('num', __('Num'));
        $form->decimal('in', __('In'))->default(0.00);
        $form->decimal('out', __('Out'))->default(0.00);
        $form->date('day', __('Day'))->default(date('Y-m-d'));

        return $form;
    }
}
